package org.nobject.common.web;

import static org.nobject.common.lang.StringUtils.isEmpty;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.nobject.common.file.XMLUtils;
import org.nobject.common.lang.StringUtils;

/**
 * Servlet工具类
 *
 * @author bianrongjun
 * @version 1.0
 */
public class ServletUtils {
	
//	/** 
//	 * 获取ServletMappingUrl
//	 * @param servletContext ServletContext
//	 * @param servletClassName Servlet类名
//	 * @throws IOException 文件寻找读取时发生的异常
//	 */
//	public static String getMappingUrl(ServletContext servletContext,String servletClassName) throws IOException{
//		return getMappingUrl(servletContext, "/WEB-INF/web.xml", servletClassName);
//	}
//	
//	/** 
//	 * 获取ServletMappingUrl
//	 * @param servletContext ServletContext
//	 * @param xmlPath web.xml路径
//	 * @param servletClassName Servlet类名
//	 * @throws IOException 文件寻找读取时发生的异常
//	 */
//	public static String getMappingUrl(ServletContext servletContext,String xmlPath,String servletClassName) throws IOException{
//		return getMappingUrl(servletContext.getResource(xmlPath).openStream(), servletClassName);
//	}
	
	/** 
	 * 获取ServletMappingUrl
	 * @param inputStream 流
	 * @param servletClassName Servlet类名
	 */
	public static String getMappingUrl(InputStream inputStream,String servletClassName){
		return getMappingUrl(XMLUtils.readDocument(inputStream), servletClassName);
	}
	
	/** 
	 * 获取ServletMappingUrl
	 * @param document 文档
	 * @param servletClassName Servlet类名
	 */
	public static String getMappingUrl(Document document,String servletClassName){
		List<Element> servlets=document.selectNodes("web-app/servlet");
		List<Element> servletMappings=document.selectNodes("web-app/servlet-mapping");
		
		if(servlets==null||servlets.size()==0
			||servletMappings==null||servletMappings.size()==0) return null;
		String servletName=null;
		for(Element element:servlets){
			Node nServeletClass=element.selectSingleNode("servlet-class");
			if(servletClassName.equals(nServeletClass.getText())){
				servletName=element.selectSingleNode("servlet-name").getText();
				break;
			}
		}
		if(servletName==null) return null;
		for(Element element:servletMappings){
			Node nServeletName=element.selectSingleNode("servlet-name");
			Node nUrlPattern=element.selectSingleNode("url-pattern");
			if(nUrlPattern!=null&&servletName.equals(nServeletName.getText())){
				servletName=element.selectSingleNode("servlet-name").getText();
				return nUrlPattern.getText();
			}
		}
		return null;
	}
	
	/**
	 * 解析URI
	 * 
	 * @param uri
	 * @return
	 */
	public static Map parseQueryString(String queryString) {
		Map<String, String> quries = new HashMap<String, String>();
		if(isEmpty(queryString)) return quries;
		String strs[] = queryString.split("&");
		String regex = "(.*)=(.*)";
		for (int i = 0; i < strs.length; i++) {
			String name = strs[i].replaceAll(regex, "$1");
			String value = strs[i].replaceAll(regex, "$2");
			if (!StringUtils.isEmpty(name)) {
				quries.put(name, value);
			}
		}
		return quries;
	}
}
